// shopMallPackage/pages/score/sendScore/sendScore.js

import { loginWxAction, sendApplyAgentCodeAction } from "../../../../service/config"
import { sendScoreAction, getMyWallet } from "../../../../service/onetoone_service"
import debounce from "../../../../tools/debounce"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponCodeBtnStatus: false,
    couponCodeBtnTitle: "获取验证码",
    snsMsgWait: 60,//倒计时
    formData: {
      code: "",
      giveAwayMoney: "",
      recPhone: "",
    },
    balanceMoney: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.myWalletInfo();
  },
  //获取我的积分数据:
  myWalletInfo() {
    getMyWallet().then(res => {
      console.log("JJLog:钱包数据===:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          balanceMoney: res.data.restMoney || 0,
        })
        app.globalData.balanceMoney = this.data.balanceMoney;
      }
    }).catch(err => {
      console.error("JJLog:getMyWallet", err);
    })
  },



  /** 发送验证码 */
  clickSendCode() {
    if (this.data.couponCodeBtnStatus) {
      return;
    }
    let that = this;
    //点击了发送按钮，就先禁用掉这个按钮点击
    this.setData({
      couponCodeBtnStatus: true,
    })
    //调用发送验证码接口:
    let phone = this.data.formData.recPhone;
    let warn = null;
    if (phone == '') {
      warn = "号码不能为空";
    } else if (phone.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone)) {
      warn = "手机号格式不正确";
    } else {
      sendApplyAgentCodeAction(phone).then((data) => {
        //当手机号正确的时候提示用户短信验证码已经发送
        wx.showToast({
          title: '短信验证码已发送',
          icon: 'none',
          duration: 2000
        });
        let inter = setInterval(function () {
          this.setData({
            couponCodeBtnStatus: true,
            couponCodeBtnTitle: this.data.snsMsgWait + 's后重发',
            snsMsgWait: this.data.snsMsgWait - 1
          });
          if (this.data.snsMsgWait < 0) {
            clearInterval(inter)
            this.setData({
              couponCodeBtnTitle: '重新发送',
              snsMsgWait: 60,
              couponCodeBtnStatus: false
            });
          }
        }.bind(this), 1000);

      }).
        catch((err) => {
          that.setData({
            couponCodeBtnStatus: false
          })
          wx.showToast({
            icon: 'none',
            title: err,
          })
        })


    }
    if (warn != null) {
      wx.showModal({
        title: '提示',
        content: warn
      })
      that.setData({
        couponCodeBtnStatus: false
      })
      return;
    }

  },
  inputAction(e) {
    let key = e.currentTarget.dataset.key;
    let changeItem = `formData.${key}`;
    this.setData({
      [changeItem]: e.detail.value
    })
  },

  sendAction() {
    let data = this.data.formData;
    if (!data.recPhone) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: "none"
      })
      return;
    }
    if (!data.code) {
      wx.showToast({
        title: '请输入验证码',
        icon: "none"
      })
      return;
    }
    if (!data.giveAwayMoney) {
      wx.showToast({
        title: '请输入转赠积分',
        icon: "none"
      })

      return;
    }
    if (data.giveAwayMoney > this.data.balanceMoney) {
      wx.showToast({
        title: '转赠积分不能大于可转赠积分',
        icon: "none"
      })
    }

    debounce(this.sendScoreRequest)
  },
  sendScoreRequest() {
    wx.showLoading();
    sendScoreAction(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '赠送成功',
          icon: "success"
        })
        this.myWalletInfo();
        this.setData({
          'formData.giveAwayMoney': 0,
        })

      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:赠送积分失败了:", err);
      wx.showToast({
        title: err || '操作失败',
        icon: "none",
        duration: 4000,
      })
    })
  },

})